import datetime

from django.shortcuts import render
from rest_framework import viewsets
from app01 import models
from app01.filterclass import OncallInfoFilter
from app01.serializer import OncallInfoSerializer
from rest_framework.response import Response
from django.shortcuts import get_object_or_404
from rest_framework.filters import SearchFilter
from rest_framework.generics import ListAPIView
from rest_framework.decorators import action


# Create your views here.


class OncallerViewset(viewsets.ViewSet, ListAPIView):
    """
    根据开始结束日期查询url:http://127.0.0.1:8000/oncaller/?start_time=2020-08-02&end_time=2021-08-02
    根据开始查询url:http://127.0.0.1:8000/oncaller/?start_time=2020-08-02
    根据组查询url:http://127.0.0.1:8000/oncaller/?dev_group=4
    """
    filter_class = OncallInfoFilter
    queryset = models.OncallInfo.objects.all()
    serializer_class = OncallInfoSerializer

    @action(methods=['GET', 'POST'], detail=False)
    def get_today(self, request):
        """
        获取当天排班，默认是获取所有排班
        url:http://127.0.0.1:8000/oncaller/get_today/
        :param request:
        :return:
        """
        queryset = models.OncallInfo.objects.filter(start_time__gte=datetime.datetime.now().date())
        print(queryset.query)
        serializer = OncallInfoSerializer(queryset, many=True)

        return Response(serializer.data)
    # def list(self, request):
    #     queryset = models.OncallInfo.objects.all()
    #     strattime = request.data.get('strattime')
    #     endtime = request.data.get('endtime')
    #     serializer = OncallInfoSerializer(queryset, many=True)
    #     return Response(serializer.data)
    #
    # def retrieve(self, request, pk=None):
    #     queryset = models.OncallInfo.objects
    #     oncaller = get_object_or_404(queryset, pk=pk)
    #     serializer = OncallInfoSerializer(oncaller)
    #     return Response(serializer.data)

    def create(self, request):
        """
        新增url:http://127.0.0.1:8000/oncaller/
        请求方法post
        :param request:
        :return:
        """
        # queryset = models.OncallInfo.objects
        print(request.data)
        serializer = OncallInfoSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data)

    def update(self, request, pk=None):
        """
        修改
        url:http://127.0.0.1:8000/oncaller/6/  6表示要删除的id值
        请求方法:put
        :param request:
        :param pk:
        :return:
        """
        queryset = models.OncallInfo.objects
        oncaller = get_object_or_404(queryset, pk=pk)
        serializer = OncallInfoSerializer(oncaller, data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data)

    def delete(self, request, pk=None):
        """
        删除
        url:http://127.0.0.1:8000/oncaller/6/  6表示要删除的id值
        请求方法:delete
        :param request:
        :param pk:
        :return:
        """
        # queryset = models.OncallInfo.objects
        # oncaller = get_object_or_404(queryset, pk=pk)
        # oncaller.delete()
        instance = models.OncallInfo.objects.filter(pk=pk).delete()
        return Response({'msg': 'deletesuccess'})
